package huoye.shops.huoye.operation.presenter;


import java.util.List;

import huoye.shops.base.BasePresenter;
import huoye.shops.base.MineBaseJsonRequestCallback;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.mine.model.ProductInfo;
import huoye.shops.huoye.operation.model.Descript;
import huoye.shops.huoye.operation.model.ShopCouponsList;
import huoye.shops.huoye.operation.model.ShopEventPrizeList;
import huoye.shops.huoye.operation.networt.OperationService;
import huoye.shops.huoye.operation.view.IBuildEventView;
import huoye.shops.huoye.operation.view.ILimitDiscountView;
import huoye.shops.huoye.user.model.User;


/**
 * Created by uesr on 2017/7/19.
 */

public class LimitDiscountPresenter extends BasePresenter<ILimitDiscountView> {


    private OperationService mService;


    //单张图片上传
    public void uploadOssImg(String id, String type, String filePath) {
        //  view.showProcessingIndicator();
        mService.uploadOssImg(id, type, filePath, new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

            @Override
            public void onSuccess(List<OssImageData> list) {
                super.onSuccess(list);
                OssImageData ossImageData = list.get(0);
                 view.setUpload(ossImageData);
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });

    }

    //多张图片上传
    public void uploadOssImgMore(String id, String type, List<String> imgUrl) {
        //  view.showProcessingIndicator();
        mService.uploadOssImgMore(id, type, imgUrl, new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

            @Override
            public void onSuccess(List<OssImageData> list) {
                super.onSuccess(list);
                view.setUploadMore(list);
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });

    }


    //图片删除
    public void deleteOssImg(String filePath) {
        //  view.showProcessingIndicator();
        mService.deleteOssImg(filePath, new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

            @Override
            public void onSuccess() {
                super.onSuccess();
                view.makeToast("OSS图片删除成功！");
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });

    }


    //商家商品上传/编辑
    public void infoProduct(String productId, String productTile, String salePrise,
                            String saleNum, String isSale, List<String> marjorImg,
                            String disType, String disNum, List<Descript> descripts,
                            String couponId, String limitStart, String limitEnd) {
        if (productTile.equals("")) {
            view.makeToast("请输入商品标题");
            return;
        } else if (salePrise.equals("")) {
            view.makeToast("请输入商品价格");
            return;
        } else if (saleNum.equals("")) {
            view.makeToast("请输入库存数量");
            return;
        }else if (saleNum.equals("0")) {
            view.makeToast("库存必须大于0");
            return;
        }else if (salePrise.equals("0")) {
            view.makeToast("商品价格不能为0");
            return;
        } else {
            mService.infoProduct(productId, productTile, salePrise, saleNum, isSale, marjorImg, disType, disNum,
                    descripts, couponId, limitStart, limitEnd,
                    new MineBaseJsonRequestCallback<User>(User.class) {
                        @Override
                        public void onSuccess() {
                            super.onSuccess();
                            if (productId.equals("0")) {
                                view.makeToast("商品上传成功");
                            } else {
                                view.makeToast("商品编辑成功");
                            }
                            view.finish();
                        }

                        //网络请求失败
                        @Override
                        public void onFailure(String msg, String code, Throwable e) {
                            super.onFailure(msg, code, e);
                            view.makeToast(msg);
                        }

                        //加载结束
                        @Override
                        public void onNetworkFail() {
                            super.onNetworkFail();
                            view.onNetworkFail();
                        }

                        //加载中
                        @Override
                        public void onFinish() {
                            super.onFinish();
                            view.hideProcessingIndicator();
                        }
                    });
        }
    }


    //商家商品删除
    public void delProduct(String productId) {
        mService.delProduct(productId,
                new MineBaseJsonRequestCallback<User>(User.class) {
                    @Override
                    public void onSuccess() {
                        super.onSuccess();
                        view.makeToast("商品删除成功");
                        view.finish();
                    }

                    //网络请求失败
                    @Override
                    public void onFailure(String msg, String code, Throwable e) {
                        super.onFailure(msg, code, e);
                        view.makeToast(msg);
                    }

                    //加载结束
                    @Override
                    public void onNetworkFail() {
                        super.onNetworkFail();
                        view.onNetworkFail();
                    }

                    //加载中
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        view.hideProcessingIndicator();
                    }
                });

    }


    //获取优惠券列表
    public void getShopGoupons() {
        mService.getShopGoupons( new MineBaseJsonRequestCallback<ShopCouponsList>(ShopCouponsList.class) {
            @Override
            public void onSuccess(List<ShopCouponsList> list) {
                super.onSuccess(list);
                view.getGouponsList(list);
            }

            //网络请求失败
                    @Override
                    public void onFailure(String msg, String code, Throwable e) {
                        super.onFailure(msg, code, e);
                        view.makeToast(msg);
                    }

                    //加载结束
                    @Override
                    public void onNetworkFail() {
                        super.onNetworkFail();
                        view.onNetworkFail();
                    }

                    //加载中
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        view.hideProcessingIndicator();
                    }
                });

    }




    @Override
    public void onStart() {
        mService = new OperationService();
    }
}
